package cn.framelife.rainbow.util.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import cn.framelife.rainbow.constant.Constant;
import cn.framelife.rainbow.db.entity.ConnectionProperty;

/**
 * 数据库连接配置解析
 * 
 * @author 潘广伟 p_3er@qq.com  
 * @date 2014-2-24 下午1:49:10
 */
public class ConnectionXMLUtil extends XMLUtil{
	
	public static void saveOrUpdate(ConnectionProperty connectionProperty) throws Exception{
		Document xmldoc = createConnectionXML();
		Element node = (Element) selectSingleNode(getExpress(connectionProperty.getConnectionName()), xmldoc.getDocumentElement());
		if(node != null){
			updateConnection(node, connectionProperty);
		}else{
			writeConnection(xmldoc,connectionProperty);
		}
		
		saveXml(Constant.DBCONNFILE, xmldoc);
	}
	
	private static void writeConnection(Document xmldoc, ConnectionProperty connectionProperty) throws Exception {
		Element connection = null, elem = null;
		Element root = xmldoc.getDocumentElement();

		// --- 新建一个连接开始 ----
		connection = xmldoc.createElement("connection");
		
		elem = xmldoc.createElement("name");
		elem.setTextContent(connectionProperty.getConnectionName());
		connection.appendChild(elem);

		elem = xmldoc.createElement("driverTemplate");
		elem.setTextContent(connectionProperty.getDriverTemplate());
		connection.appendChild(elem);
		
		elem = xmldoc.createElement("jarURL");
		elem.setTextContent(connectionProperty.getJarURL());
		connection.appendChild(elem);

		elem = xmldoc.createElement("driverClass");
		elem.setTextContent(connectionProperty.getDriverClass());
		connection.appendChild(elem);

		elem = xmldoc.createElement("connectionURL");
		elem.setTextContent(connectionProperty.getConnectionURL());
		connection.appendChild(elem);

		elem = xmldoc.createElement("username");
		elem.setTextContent(connectionProperty.getUsername());
		connection.appendChild(elem);

		elem = xmldoc.createElement("password");
		elem.setTextContent(connectionProperty.getPassword());
		connection.appendChild(elem);
		
		elem = xmldoc.createElement("databaseName");
		elem.setTextContent(getDatabaseName(connectionProperty.getDriverTemplate(), connectionProperty.getConnectionURL()));
		connection.appendChild(elem);
		
		connection.setAttribute("id", connectionProperty.getConnectionName());
		root.appendChild(connection);
		
		// --- 新建一个连接完成 ----
		
		
	}
	
	/**
	 * 更新配置文件
	 *
	 * @param editId
	 * @param driverTemplate
	 * @param connectionName
	 * @param jarURL
	 * @param driverClass
	 * @param connectionURL
	 * @param username
	 * @param password
	 * @throws Exception
	 */
	private static void updateConnection(Element node, ConnectionProperty connectionProperty) throws Exception{
		XMLUtil.updateValue(node, "name", connectionProperty.getConnectionName());
		XMLUtil.updateValue(node, "driverTemplate", connectionProperty.getDriverTemplate());
		XMLUtil.updateValue(node, "jarURL", connectionProperty.getJarURL());
		XMLUtil.updateValue(node, "driverClass", connectionProperty.getDriverClass());
		XMLUtil.updateValue(node, "connectionURL", connectionProperty.getConnectionURL());
		XMLUtil.updateValue(node, "username", connectionProperty.getUsername());
		XMLUtil.updateValue(node, "password", connectionProperty.getPassword());
		XMLUtil.updateValue(node, "databaseName", getDatabaseName(connectionProperty.getDriverTemplate(), connectionProperty.getConnectionURL()));
		node.setAttribute("id", connectionProperty.getConnectionName());
	}

	/**
	 * 获取数据库的名称
	 *
	 * @param driverTemplate
	 * @param connectionURL
	 * @return
	 */
	private static String getDatabaseName(String driverTemplate,
			String connectionURL) {
		String databaseName = "";
		String[] arr = null;
		if(driverTemplate.equals("MySQL connector/J")){
			arr = connectionURL.split("/");
		}else if(driverTemplate.equals("Oracle (Thin driver)")){
			arr = connectionURL.split(":");
		}else if(driverTemplate.equals("Oracle (Thin driver)")){
			arr = connectionURL.split("@");
			
		}
		if(arr!= null && arr.length > 0){
			databaseName = arr[arr.length - 1];
		}
		return databaseName;
	}
	
	
	

	/**
	 * 创建一个数据库配置文件
	 *
	 * @return
	 * @throws Exception
	 */
	public static Document createConnectionXML()
			throws Exception {
		createDataPath();
		//创建文档树模型对象
		DocumentBuilder dbBuilder = createBuilder();
		Document xmldoc = dbBuilder.newDocument();
		
		createDataPath();
		File file = new File(Constant.DBCONNFILE);
		
		if(file.exists()){
			xmldoc = dbBuilder.parse(file);
		}else{
			file.createNewFile();
			Element connecions = xmldoc.createElement("connections");
			xmldoc.appendChild(connecions);
			saveXml(Constant.DBCONNFILE, xmldoc);
		}
		
		return xmldoc;
	}

	public static void deleteConnection(String id) throws Exception{
		deleteNode(getExpress(id), createConnectionXML(), Constant.DBCONNFILE);
	}
	
	public static Element getSelectionConnection(String id) throws Exception{
		return (Element) selectSingleNode(getExpress(id), createConnectionXML().getDocumentElement());
	}

	public static String getExpress(String id){
		StringBuffer express = new StringBuffer();
		express.append("/connections/connection[@id='")
		       .append(id).append("']");
		return express.toString();
	}
	
	
}
